package com.sun.dream.fish.frontmodel.resources;

import io.micrometer.common.util.StringUtils;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

import java.util.Locale;

/**
 * Description:  加载系统资源
 * Created By ljl on 2023/12/07.
 */
public abstract class DSFMessageLoader {
    private final ReloadableResourceBundleMessageSource messageSource;

    protected DSFMessageLoader(String... baseNames) {
        messageSource = new ReloadableResourceBundleMessageSource();
        messageSource.setBasenames(baseNames);
        messageSource.setDefaultEncoding("UTF-8");
        messageSource.setUseCodeAsDefaultMessage(true);
    }

    public String getMessageByLocale(String key, Locale locale, Object... message) {
        return messageSource.getMessage(key, message, null, locale);
    }

    public String getMessageByLocale(String key, String locale, Object... message) {
        return getMessageByLocale(key, locale(locale), message);
    }

    protected Locale locale(String locale) {
        try {
            return StringUtils.isNotBlank(locale) ? Locale.forLanguageTag(locale) : Locale.CHINA;
        } catch (Exception e) {
            return Locale.CHINA;
        }
    }
}
